def writetxt(filename, line):
    with open(filename, 'a', encoding='utf-8') as w:
        w.write(line)


def conv2txt(filename):
    with open(filename, 'r', encoding='utf-8') as f:
        lines = f.readlines()
        print("lines read over")
        num = 0
        for i in range(0, len(lines)-3, 3):
            if lines[i] == 'E\n' and lines[i+3] == 'E\n':

                lineA_ = ' '.join(lines[i+1][2:]).replace('\n', '')
                lineB_ = ' '.join(lines[i+2][2:]).replace('\n', '')

                if lineA_.replace(' ','').replace('=','').replace('。', '') == '' \
                        or lineB_.replace(' ','').replace('=','').replace('。', '') == '':
                    continue
                else:
                    lineA = 'BOS '+lineA_ + 'PAD\n'
                    lineB = 'BOS '+lineB_ + 'PAD\n'
                    num = num + 1
                    writetxt('data/sentence.txt', lineA)
                    writetxt('data/label.txt', lineB)
                    if i % 100 == 0:
                        print('step:%s\n%s%s'%(i, lineA, lineB))
                    if num == 159200:
                        break


conv2txt('data/xiaohuangji50w.conv')
